package com.jingdianjichi.subject.controller;


import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.controller.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.controller.dto.SubjectCategoryDTO;

import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *分类Controller
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增分类
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToCategoryBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
        } catch (Exception e) {
            log.info("添加任务时失败，捕获异常");
            return Result.fail("添加失败");
        }
        log.info("添加成功");
        return Result.success(true);


    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                .convertDtoToCategoryBO(subjectCategoryDTO);
        List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
        List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBOList);
        return Result.success(subjectCategoryDTOList);
    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                .convertDtoToCategoryBO(subjectCategoryDTO);
        List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
        List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBOList);
        return Result.success(subjectCategoryDTOList);
    }

    /**
     * 更新分类
     */

    @PostMapping("/update")
    public Result<Boolean> upDate(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                .convertDtoToCategoryBO(subjectCategoryDTO);
        Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);
        return Result.success(result);
    }

    /**
     * 删除分类
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
                .convertDtoToCategoryBO(subjectCategoryDTO);
        Boolean result = subjectCategoryDomainService.delete(subjectCategoryBO);
        return Result.success(result);
    }
}

